package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.coupon.CouponConfDto;
import com.java110.po.coupon.CouponConfPo;
import com.java110.mall.shop.bmo.couponConf.IDeleteCouponConfBMO;
import com.java110.mall.shop.bmo.couponConf.IGetCouponConfBMO;
import com.java110.mall.shop.bmo.couponConf.ISaveCouponConfBMO;
import com.java110.mall.shop.bmo.couponConf.IUpdateCouponConfBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/couponConf")
public class CouponConfApi {

    @Autowired
    private ISaveCouponConfBMO saveCouponConfBMOImpl;
    @Autowired
    private IUpdateCouponConfBMO updateCouponConfBMOImpl;
    @Autowired
    private IDeleteCouponConfBMO deleteCouponConfBMOImpl;

    @Autowired
    private IGetCouponConfBMO getCouponConfBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /couponConf/saveCouponConf
     * @path /app/couponConf/saveCouponConf
     */
    @RequestMapping(value = "/saveCouponConf", method = RequestMethod.POST)
    public ResponseEntity<String> saveCouponConf(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "confId", "请求报文中未包含confId");
        Assert.hasKeyAndValue(reqJson, "vipLevelId", "请求报文中未包含vipLevelId");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "spId", "请求报文中未包含spId");
        Assert.hasKeyAndValue(reqJson, "couponName", "请求报文中未包含couponName");
        Assert.hasKeyAndValue(reqJson, "actualPrice", "请求报文中未包含actualPrice");
        Assert.hasKeyAndValue(reqJson, "couponCount", "请求报文中未包含couponCount");
        Assert.hasKeyAndValue(reqJson, "poolId", "请求报文中未包含poolId");


        CouponConfPo couponConfPo = BeanConvertUtil.covertBean(reqJson, CouponConfPo.class);
        return saveCouponConfBMOImpl.save(couponConfPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /couponConf/updateCouponConf
     * @path /app/couponConf/updateCouponConf
     */
    @RequestMapping(value = "/updateCouponConf", method = RequestMethod.POST)
    public ResponseEntity<String> updateCouponConf(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "confId", "请求报文中未包含confId");
        Assert.hasKeyAndValue(reqJson, "vipLevelId", "请求报文中未包含vipLevelId");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "spId", "请求报文中未包含spId");
        Assert.hasKeyAndValue(reqJson, "couponName", "请求报文中未包含couponName");
        Assert.hasKeyAndValue(reqJson, "actualPrice", "请求报文中未包含actualPrice");
        Assert.hasKeyAndValue(reqJson, "couponCount", "请求报文中未包含couponCount");
        Assert.hasKeyAndValue(reqJson, "poolId", "请求报文中未包含poolId");
        Assert.hasKeyAndValue(reqJson, "confId", "confId不能为空");


        CouponConfPo couponConfPo = BeanConvertUtil.covertBean(reqJson, CouponConfPo.class);
        return updateCouponConfBMOImpl.update(couponConfPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /couponConf/deleteCouponConf
     * @path /app/couponConf/deleteCouponConf
     */
    @RequestMapping(value = "/deleteCouponConf", method = RequestMethod.POST)
    public ResponseEntity<String> deleteCouponConf(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "caId", "caId不能为空");

        Assert.hasKeyAndValue(reqJson, "confId", "confId不能为空");


        CouponConfPo couponConfPo = BeanConvertUtil.covertBean(reqJson, CouponConfPo.class);
        return deleteCouponConfBMOImpl.delete(couponConfPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param
     * @return
     * @serviceCode /couponConf/queryCouponConf
     * @path /app/couponConf/queryCouponConf
     */
    @RequestMapping(value = "/queryCouponConf", method = RequestMethod.GET)
    public ResponseEntity<String> queryCouponConf(@RequestParam(value = "shopId", required = false) String shopId,
                                                  @RequestParam(value = "page") int page,
                                                  @RequestParam(value = "row") int row) {

        CouponConfDto couponConfDto = new CouponConfDto();
        couponConfDto.setPage(page);
        couponConfDto.setRow(row);
        couponConfDto.setShopId(shopId);
        return getCouponConfBMOImpl.get(couponConfDto);
    }
}
